package com.maxd.controller.activity;
import com.maxd.adapter.IgnoreSecurity;
import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.model.Activity;
import com.maxd.respository.activity.ActivityJpaRepository;
import com.maxd.respository.activity.ActivityRepository;
import com.maxd.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(value="菜单和活动管理",tags={"菜单和活动管理"})
@RequestMapping(value = "/activity")
public class ActivityController {
    @Autowired
    private ActivityJpaRepository activityJpaRepository;

    @Autowired
    private ActivityRepository activityRepository;


    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiOperation("管理平台详情")
    @ResponseBody
    public Result getBanner(@PathVariable Integer id) {
        return ResultUtil.success(activityJpaRepository.findById(Long.valueOf(id)).orElse(null));
    }

    @RequestMapping(value = "/state/{state}", method = RequestMethod.GET)
    @ApiOperation("根据状态查询菜单列表")
    @IgnoreSecurity
    @ResponseBody
    public Result getBannerState(@PathVariable String state,Integer page,Integer size,Integer type) {
        if(page == null || size ==null){
            if(type==null){
                return ResultUtil.success(activityRepository.findOne(state));
            }
            return ResultUtil.success(activityRepository.findOne(state,type));
        }else{
            Pageable pageable= PageRequest.of(page,size);
            if(type==null){
                return ResultUtil.success(activityRepository.findPage(pageable,state));
            }
            return ResultUtil.success(activityRepository.findPage(pageable,state,type));
        }
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ApiOperation("管理平台添加")
    @ResponseBody
    public Result addBanner(@PathVariable String id, @RequestBody Activity info) {
        Activity activity = new Activity();
        activity.setId(Long.valueOf(id));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        activity.setImage_url(info.getImage_url());
        activity.setTitle(info.getTitle());
        activity.setCreateAt(sdf.format(now));
        activity.setState(info.getState());
        activity.setUrl(info.getUrl());
        activity.setType(info.getType());
        activity.setSort(info.getSort());
        activity.setBackgroundImage(info.getBackgroundImage());
        return ResultUtil.success(activityJpaRepository.save(activity));
    }

    @PostMapping(value = "/save")
    @ApiOperation("管理平台添加")
    public Result saveBanner(@RequestBody Activity info) {
        info.setCreateAt(DateUtil.createTime());
        return ResultUtil.success(activityJpaRepository.save(info));
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ApiOperation("管理平台删除")
    public Result deleteBanner(@PathVariable Long id) {
        activityJpaRepository.deleteById(id);
        return ResultUtil.success();
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ApiOperation("管理平台获取全部广告位")
    @ResponseBody
    public Result getBannerList() {
        return ResultUtil.success(activityJpaRepository.findAll());
    }


}
